﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace Crgd.UAuthServer.T4.Entities
{
    /// <summary>
    /// 表结构
    /// </summary>
    public sealed class DbTable
    {
        /// <summary>
        /// 模块名称
        /// </summary>
        public string MoudleName { get; set; }
        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName { get; set; }
        /// <summary>
        /// 表的架构
        /// </summary>
        public string SchemaName { get; set; }
        /// <summary>
        /// 表的记录数
        /// </summary>
        public int Rows { get; set; }

        /// <summary>
        /// 是否含有主键
        /// </summary>
        public bool HasPrimaryKey { get; set; }

        /// <summary>
        /// 是否只读，用于视图
        /// </summary>
        public bool IsReadonly { get; set; }

        /// <summary>
        /// 中文描述
        /// </summary>
        public string Remark { get; set; }

        /// <summary>
        /// 列信息列表
        /// </summary>
        public IList<DbColumn> Columns { get; set; }

        public bool IsCompositePrimaryKey
        {
            get
            {
                return Columns.Count(p => p.IsPrimaryKey) > 1;
            }
        }

        private bool? _isDEntityBase = null;
        public bool IsDEntityBase
        {
            get
            {
                if (_isDEntityBase == null)
                {
                    _isDEntityBase = Columns.Count(p => DEntityBaseColumns.Contains(p.ColumnName)) == DEntityBaseColumns.Count();
                }
                return _isDEntityBase.Value;
            }
        }

        private bool? _isDataPermissionsColumns = null;
        public bool IsDataPermissionsColumns
        {
            get
            {
                if (_isDataPermissionsColumns == null)
                {
                    _isDataPermissionsColumns = Columns.Count(p => DataPermissionsColumns.Contains(p.ColumnName)) == DataPermissionsColumns.Count();
                }
                return _isDataPermissionsColumns.Value;
            }
        }

        private bool? _isDEntityTenant = null;
        public bool IsDEntityTenant
        {
            get
            {
                if(_isDEntityTenant == null)
                {
                    _isDEntityTenant = IsDEntityBase && Columns.Count(p => p.ColumnName == "TenantId") > 0;
                }
                return _isDEntityTenant.Value;
            }
        }

        public static string[] DEntityBaseColumns
        {
            get { return new string[] { "Id", "CreatedTime", "UpdatedTime", "CreatedUserId", "CreatedUserName", "UpdatedUserId", "UpdatedUserName", "IsDeleted" }; }
        }

        public static string[] DataPermissionsColumns
        {
            get { return new string[] { "CreatedUserOrgId", "CreatedUserOrgName", "CreatedUserId", "CreatedUserName" }; }
        }

        public string TableBaseClassAndInterfaceName(string entityName)
        {
            var result = new StringBuilder();

            if (IsDEntityTenant)
            {
                result.Append("DEntityTenant");
            }
            else if (IsDEntityBase)
            {
                result.Append("DEntityBase");
            }
            else
            {
                result.Append("IEntity");
            }

            result.Append($", IEntityTypeBuilder<{entityName}>");

            if (IsDataPermissionsColumns)
            {
                result.Append(", IDataPermissions");
            }

            return result.ToString();
        }

        public bool IsBaseClassColumn(string columnName)
        {
            if (columnName == null) return false;

            var result = false;
            if (IsDEntityTenant)
            {
                result = columnName == "TenantId" || DEntityBaseColumns.Contains(columnName);
            }

            if (!result && IsDEntityBase)
            {
                result = DEntityBaseColumns.Contains(columnName);
            }

            //if(!result && IsDataPermissionsColumns)
            //{
            //    result |= DataPermissionsColumns.Contains(columnName);
            //}

            return result;
        }
    }
}
